import store from '@/store'
import Utils from '@/utils'
const state = {
  // 登录信息
  loginInfo: null,
  // 选中商家
  selectBusiness: null,
  // 是否显示扫码点单 （ 用于审核检查 ）
  ifScan: true
}

const mutations = {
  SetLoginInfo: (state, loginInfo) => {
    state.loginInfo = loginInfo
    uni.setStorageSync('loginInfo', loginInfo)
  },
  // 选中商家
  SetSelectBusiness: (state, selectBusiness) => {
    state.selectBusiness = selectBusiness
    uni.setStorageSync('selectBusiness', selectBusiness)
  },
  // 初始化登录状态
  InitLoginInfo: state => {
    state.loginInfo = null
    uni.setStorageSync('loginInfo', null)
  },
  // 初始化选中商家
  InitSelectBusiness: state => {
    state.selectBusiness = null
    uni.setStorageSync('selectBusiness', null)
  },
  // 控制是否显示扫码点单
  SetIfScan: (state, ifScan) => {
    state.ifScan = ifScan
    uni.setStorageSync('ifScan', null)
  }
}

const actions = {
  /**
   * 选中商家
   * 需要 ： 初始化门店列表 + 选中门店 + 房台列表 + 选中房台 + loginInfo
   */
  setSelectBusiness({ commit }, selectBusiness) {
    const gettersSelectBusiness = store.getters.selectBusiness
    const gettersUniacid = Utils.format(gettersSelectBusiness, 'selectuniacid')

    // 选中跟上一次不同的商家才需要初始化
    if (Utils.format(selectBusiness, 'selectuniacid') !== gettersUniacid) {
      commit('InitLoginInfo')
      commit('SetSelectBusiness', selectBusiness) // 选中商家
    }
  },
  // 登录信息 + 商家信息 + 房台信息
  setLoginInfo({ commit }, loginInfo) {
    commit('InitSelectBusiness')
    commit('SetLoginInfo', loginInfo)
  },
  // 控制是否显示扫码点单按钮
  setIfScan({ commit }, ifScan) {
    commit('SetIfScan', ifScan)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
